/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * PTDMachinePanel.java
 *
 * Created on August 26, 2008, 9:43 AM
 */

package ptd;

import java.awt.Color;
import java.awt.Container;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JOptionPane;

/**
 *
 * @author  John
 */
public class PTDMachinePanel extends javax.swing.JPanel {

    PTDRemoteMachine Machine;
    
    /** Creates new form PTDMachinePanel */
    public PTDMachinePanel() {
        initComponents();
        ImageIcon a = PTDImageLoader.getImageIcon(PTDImageLoader.IMG_STOP_SCRIPT);
        StopButton.setIcon(a);
        a = PTDImageLoader.getImageIcon(PTDImageLoader.IMG_DISPOSE);
        DisposeButton.setIcon(a);
        
    }
    
    public void setMachine(PTDRemoteMachine In){
        Machine = In;
        Machine.StatusPanel = this;
        MachineLabel.setText(Machine.myName);        
    }

    public void setCaseLabel(String Case){
        CaseLabel.setText("Case: " + Case);
    }
    
    public void setStatusLabel(String Status){
        StatusLabel.setText("Status: " + Status);
        if(Status.compareTo("Running") == 0){
            StatusLabel.setForeground(Color.blue);
        }else if(Status.compareTo("Stopped") == 0){
            StatusLabel.setForeground(Color.RED);
        }else if(Status.compareTo("Finished") == 0){
            StatusLabel.setForeground(Color.GRAY);
            CaseLabel.setForeground(Color.GRAY);
            MachineLabel.setForeground(Color.GRAY);
            StopButton.setEnabled(false);
        }else{
            StatusLabel.setForeground(Color.BLACK);
        }
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        StopButton = new javax.swing.JButton();
        MachineLabel = new javax.swing.JLabel();
        StatusLabel = new javax.swing.JLabel();
        CaseLabel = new javax.swing.JLabel();
        DisposeButton = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        StopButton.setToolTipText("Stop run");
        StopButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        StopButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                StopButtonMouseClicked(evt);
            }
        });
        StopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StopButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(StopButton, gridBagConstraints);

        MachineLabel.setBackground(new java.awt.Color(255, 255, 255));
        MachineLabel.setToolTipText("Machine Name");
        MachineLabel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2)));
        MachineLabel.setMaximumSize(new java.awt.Dimension(120, 0));
        MachineLabel.setMinimumSize(new java.awt.Dimension(120, 0));
        MachineLabel.setOpaque(true);
        MachineLabel.setPreferredSize(new java.awt.Dimension(120, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(MachineLabel, gridBagConstraints);

        StatusLabel.setBackground(new java.awt.Color(255, 255, 255));
        StatusLabel.setToolTipText("Run status");
        StatusLabel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2)));
        StatusLabel.setMaximumSize(new java.awt.Dimension(120, 0));
        StatusLabel.setMinimumSize(new java.awt.Dimension(120, 0));
        StatusLabel.setPreferredSize(new java.awt.Dimension(120, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(StatusLabel, gridBagConstraints);

        CaseLabel.setBackground(new java.awt.Color(255, 255, 255));
        CaseLabel.setToolTipText("Current run case");
        CaseLabel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2)));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(CaseLabel, gridBagConstraints);

        DisposeButton.setToolTipText("Remove run from list");
        DisposeButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        DisposeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DisposeButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(DisposeButton, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

private void StopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StopButtonActionPerformed
    stopMe();
}//GEN-LAST:event_StopButtonActionPerformed

private void StopButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_StopButtonMouseClicked
    
}//GEN-LAST:event_StopButtonMouseClicked

private void DisposeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DisposeButtonActionPerformed
    disposeMe();
}//GEN-LAST:event_DisposeButtonActionPerformed

public void disposeMe(){
    
    if(Machine.runState){
        JOptionPane Option = new JOptionPane();
        Option.setMessageType(Option.QUESTION_MESSAGE);
        Option.setOptionType(Option.YES_NO_OPTION);
        int iSel = Option.showConfirmDialog(null, "This will end remote execution on [" + Machine.myName + "].  Continue?", "PTD", Option.YES_NO_OPTION);                       
        if(iSel == JOptionPane.YES_OPTION){
            if(Machine.CurrentThread != null){
                Machine.CurrentThread.Kill();
            }else{
                Machine.runState = false;
            }
        }else{
            return;  
        }
    }
    
    Container P = getParent();
    P.remove(this);
    P.invalidate();
    P.validate();
    P.repaint();
}

public void stopMe(){
    Machine.runState = false;
}


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel CaseLabel;
    private javax.swing.JButton DisposeButton;
    private javax.swing.JLabel MachineLabel;
    public javax.swing.JLabel StatusLabel;
    private javax.swing.JButton StopButton;
    // End of variables declaration//GEN-END:variables

}
